package com.um.jdy.common.charge.service.impl;

import com.um.jdy.common.charge.dao.OrderSharingUserDao;
import com.um.jdy.common.charge.entity.po.Order;
import com.um.jdy.common.charge.entity.po.OrderSharingUser;
import com.um.jdy.common.device.dao.StationDao;
import com.um.jdy.common.device.dao.StationSharingDao;
import com.um.jdy.common.device.entity.po.Station;
import com.um.jdy.common.device.entity.po.StationSharing;
import com.um.jdy.common.platform.dao.MerchantDao;
import com.um.jdy.common.platform.entity.po.Merchant;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;
import com.um.springboot.starter.orm.service.BaseEntityServiceImpl;
import com.um.jdy.common.charge.service.OrderSharingService;
import com.um.jdy.common.charge.dao.OrderSharingDao;
import com.um.jdy.common.charge.entity.po.OrderSharing;

import java.util.Date;
import java.util.List;

/**
 * 服务实现类：充电订单分账表
 */
@Service
@Slf4j
public class OrderSharingServiceImpl extends BaseEntityServiceImpl<OrderSharingDao,OrderSharing>  implements OrderSharingService{

    @Autowired
    StationDao stationDao;

    @Autowired
    MerchantDao merchantDao;

    @Autowired
    StationSharingDao stationSharingDao;

    @Autowired
    OrderSharingUserDao orderSharingUserDao;

    /**
     * 记录订单分佣的副本
     *
     * @param order
     */
    @Override
    public void addOrderSharing(Order order) {

        Station station = stationDao.getRow(order.getDeviceStationId());
        Merchant merchant = merchantDao.getRow(order.getPlatformMerchantId());
        Merchant parent = merchantDao.getRow(merchant.getParentId());


        OrderSharing orderSharing  = new OrderSharing();
        orderSharing.setComId(order.getComId());
        orderSharing.setChargeOrderId(order.getChargeOrderId());
        orderSharing.setCreateTime(new Date());

        if(parent != null) {
            orderSharing.setParentId(parent.getPlatformMerchantId());
            orderSharing.setParentRate(parent.getCommissionRate());
        }



        if(merchant != null) {
            orderSharing.setMerchantId(merchant.getPlatformMerchantId());
        }

        if(station != null) {
            orderSharing.setMerchantRate(station.getCommissionRate());

            List<StationSharing> stationSharingList = stationSharingDao.getRows(new String[]{"device_station_id"}, new Object[]{station.getDeviceStationId()},
                    new String[]{}, new String[]{});
            for (StationSharing item : stationSharingList) {

                OrderSharingUser orderSharingUser = new OrderSharingUser();
                orderSharingUser.setComId(order.getComId());
                orderSharingUser.setChargeOrderId(order.getChargeOrderId());
                orderSharingUser.setPlatformMerchantId(item.getPlatformMerchantId());
                orderSharingUser.setCommissionRate(item.getCommissionRate());
                orderSharingUser.setCreateTime(new Date());
                orderSharingUserDao.saveRow(orderSharingUser);
            }
        }

        this.save(orderSharing);

    }
}
